﻿using AutoMapper;
using FreeRedis;
using Simple.EntityFramewokCore.Core.Base;
using Video.Application.Contract.Base;
using Video.Application.Contract.UserInfos;
using Video.Application.Contract.UserInfos.Dtos;
using Video.Application.Manage;
using Video.Domain.Shared;
using Video.Domain.Users;

namespace Video.Application.UserInfos;

public class UserInfoService : IUserInfoService
{
    private readonly IMapper _mapper;
    private readonly RedisClient _redisClient;
    private readonly IUserInfoRepository _userInfoRepository;
    private readonly CurrentManage _currentManage;
    private readonly IUnitOfWork _unitOfWork;

    public UserInfoService(IMapper mapper, RedisClient redisClient, IUserInfoRepository userInfoRepository, CurrentManage currentManage, IUnitOfWork unitOfWork)
    {
        _mapper = mapper;
        _redisClient = redisClient;
        _userInfoRepository = userInfoRepository;
        _currentManage = currentManage;
        _unitOfWork = unitOfWork;
    }

    public async Task DeleteAsync(IEnumerable<Guid> id)
    {
        await _userInfoRepository.DeleteAsync(id);
    }

    public async Task ForbiddenAsync(IEnumerable<Guid> id)
    {
        await _userInfoRepository.ForbiddenAsync(id);
    }

    public async Task<UserInfoRoleDto> GetAsync()
    {
        var data = await _userInfoRepository.GetAsync(_currentManage.GetId());
        var dto = _mapper.Map<UserInfoRoleDto>(data);

        return dto;
    }

    /// <summary>
    /// 获取用户列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public Task<PageResultDto<UserInfoDto>> GetListAsync(GetListInput input)
    {
        throw new NotImplementedException();
    }

    public async Task<UserInfoRoleDto> LoginAsync(LoginInput input)
    {
        var data = await _userInfoRepository.GetUserInfoRoleAsync(input.UserName, input.Password);
        var dto = _mapper.Map<UserInfoRoleDto>(data);
        return dto;
    }

    /// <summary>
    /// 注册用户
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public async Task<UserInfoRoleDto> RegisterAsync(RegisterInput input)
    {
        var code = await _redisClient.GetAsync<string>($"{CodeType.Register}:{input.UserName}");
        if (code != input.Code)
        {
            throw new BusinessException("验证码错误");
        }


        if (await _userInfoRepository.IsExistAsync(x => x.UserName == input.UserName))
        {
            throw new BusinessException("用户名已经存在");
        }
        var data = _mapper.Map<UserInfo>(input);
        data = await _userInfoRepository.InsertAsync(data);

        await _unitOfWork.SaveChangesAsync();

        return _mapper.Map<UserInfoRoleDto>(data);
    }

    /// <summary>
    /// 编辑用户信息
    /// </summary>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public async Task UpdateAsync(UpdateUserInfoInput input)
    {
        var userInfo = await _userInfoRepository.FirstOrDefaultAsync(x => x.Id == _currentManage.GetId());
        if (userInfo == null)
        {
            throw new BusinessException("用户信息不存在");
        }

        userInfo.Avatart = input.Avatar;
        userInfo.Name = input.Name;
        userInfo.Password = input.Password;

        await _userInfoRepository.UpdateAsync(userInfo);

        await _unitOfWork.SaveChangesAsync();
    }
}
